package org.fing.edu.uy.esb.adaptative.ws.egov.dnic;

import java.net.InetAddress;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.jws.soap.SOAPBinding.Style;
import javax.jws.soap.SOAPBinding.Use;
import javax.xml.ws.Endpoint;
import javax.xml.ws.soap.Addressing;

import org.fing.edu.uy.esb.adapative.ws.testing.PrintName;
import org.fing.edu.uy.esb.adaptative.ws.utils.WsOptionProces;


@Addressing()
@WebService(serviceName="DNICWS")
@SOAPBinding(parameterStyle=ParameterStyle.WRAPPED, style=Style.DOCUMENT, use=Use.LITERAL)
public class DNICWS {
	
	private Endpoint endpoint;
	private Endpoint endpoint2;
	private final int version = 1;
	
	@WebMethod(exclude=true)
	public void publish(int port , String wsName){
		try{
			InetAddress address = InetAddress.getLocalHost();			
			String url = "http://" + address.getHostName() + ":" + port + "/" + wsName;
			endpoint = Endpoint.publish(url, this);		
			System.out.println("Service v"+ version +" published at:" + url);
			url = "http://localhost:" + port + "/" + wsName;
			endpoint2 = Endpoint.publish(url, this);		
			System.out.println("Service v"+ version +" published at:" + url);
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in publish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	@WebMethod(exclude=true)
	public void unpublish(){
		try{
			if(endpoint!=null && endpoint.isPublished()){
				endpoint.stop();
				endpoint2.stop();
				System.out.println("Service stopped");
			}
		}catch (Exception ex) {
			Logger.getLogger(this.getClass().getSimpleName()).log(Level.SEVERE, "Exception in unpublish " + this.getClass().getSimpleName(), ex);
		}
	}
	
	
	
	
	/**
	 * 
	 * @param CI = [ ].[   ].[   ]-[ ]
	 * @param digVer
	 * @return retorna un objeto que representa a la persona. si digVer es -1 no se hace control de digito de verificacion
	 */
	@WebMethod()
	public PersonaDNIC obtenerInformacionPersona(String cedula, String digVer){
		PrintName.printMethodName();
		
		PersonaDNIC persona = null;
		
		Pattern patronCedula = Pattern.compile("^(\\d)(\\d\\d\\d)(\\d\\d\\d)");
		Matcher matcherCedula = patronCedula.matcher(cedula);
		Pattern patronDigVerif = Pattern.compile("^(\\d)");
		Matcher matcherDigVerif = patronDigVerif.matcher(digVer);
		if (matcherCedula.find() && matcherDigVerif.find()) {
			String millones = matcherCedula.group(1);
			String miles = matcherCedula.group(2);
			String cientos = matcherCedula.group(3);
				
			persona = new PersonaDNIC();
		
			persona.setApellidoMaterno("apellidoMaterno (DNIC)");
			persona.setApellidoPaterno("apellidoPaterno (DNIC)");
			persona.setCI(millones + "." + miles + "." + cientos + "-" + digVer);
			persona.setFirmaCedula("");
			persona.setPrimerNombre("primerNombre (DNIC)");
			persona.setSegundoNombre("segundoNombre (DNIC)");
			persona.setSexo("sexo (DNIC)");		
			
		}
		WsOptionProces.processDNIC();
		return persona;
	}
	
	

}
